package com.atguigu.gulimall.order.web;

import com.alipay.api.AlipayApiException;
import com.atguigu.gulimall.order.config.AlipayTemplate;
import com.atguigu.gulimall.order.service.OrderService;
import com.atguigu.gulimall.order.vo.PayAsyncVo;
import com.atguigu.gulimall.order.vo.PayVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;

@Controller
public class PayWebController {
    @Autowired
    AlipayTemplate alipayTemplate;
    @Autowired
    OrderService orderService;

    /**
     * 正常集成支付宝支付的源码，因为没有集成，所以暂时注释掉，直接跳转到我的订单页面
     * @param orderSn
     * @return
     * @throws AlipayApiException
     */
//    @GetMapping(value = "/payOrder", produces = "text/html")
//    public String payOrder(@RequestParam("orderSn") String orderSn) throws AlipayApiException {
//        PayVo vo = orderService.getPayVo(orderSn);
//        //支付宝返回的是一个form表单，并且自动submit
//        //submit后，会跳转到配置的AlipayTemplate.return_url
//        String pay = alipayTemplate.pay(vo);
//        return pay;
//    }

    @GetMapping(value = "/payOrder", produces = "text/html")
    public String payOrder(@RequestParam("orderSn") String orderSn) throws AlipayApiException {
        //因为没有集成支付宝，原本在监听支付宝异步通知支付成功后才调用的订单逻辑，放在这直接调用
        PayAsyncVo vo=new PayAsyncVo();
        vo.setTrade_no(orderSn);
        vo.setOut_trade_no(orderSn);
        vo.setTrade_status("TRADE_SUCCESS");
        vo.setNotify_time(new Date());
        orderService.handlePayResult(vo);
        return "redirect:http://member.gulimall.com/memberOrder.html";
    }
}
